import axios from "axios"
import { constants } from "."

const changeLogin = () => ({
  type: constants.CHANGE_LOGIN_SUCCESS,
  payload: true
})

export const logout = () => ({
  type: constants.LOGOUT,
  payload: false
})

export const login = (account, password) => {
  return (dispatch) => {
    // 这里为了模拟使用get请求，实际开发应为post
    axios.get(`/api/login.json?account=${account}&password=${password}`)
      .then(response => {
        const { data: { data } } = response;
        if(data) {
          dispatch(changeLogin())
        } else {
          alert('登录失败');
        }
      },
      error => { console.log('login error', error) }
      )
  }
}